package com.ruoyi.work.service.impl;


import com.github.pagehelper.PageHelper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.work.domain.Product;
import com.ruoyi.work.mapper.ProductMapper;
import com.ruoyi.work.query.ProductQuery;
import com.ruoyi.work.query.TablePageInfo;
import com.ruoyi.work.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;


    @Override
    public TablePageInfo<Product> query(ProductQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Product>(productMapper.selectForList(qo));
    }


    @Override
    public void save(Product product) {
        productMapper.insert(product);
    }

    @Override
    public Product get(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Product product) {
       return productMapper.updateByPrimaryKey(product);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            productMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Product> list() {
        return productMapper.selectAll();
    }

    @Override
    public int insertArticle(String name, Integer price, Integer salePrice, String brand, String code, MultipartFile picture) throws IOException {
        Product article = new Product();
        article.setPicture(FileUploadUtils.upload(RuoYiConfig.getAvatarPath(),picture));
        article.setName(name);
        article.setPrice(price);
        article.setSalePrice(salePrice);
        article.setBrand(brand);
        article.setCode(code);
        return productMapper.insert(article);

    }

    @Override
    public int update2(String name, Integer price, Integer salePrice, String brand, String code, MultipartFile picture) throws IOException {
        Product article = new Product();
        article.setPicture(FileUploadUtils.upload(RuoYiConfig.getAvatarPath(),picture));
        article.setName(name);
        article.setPrice(price);
        article.setSalePrice(salePrice);
        article.setBrand(brand);
        article.setCode(code);
        return productMapper.updateByPrimaryKey(article);
    }
}
